﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataObject;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.BusinessObject
{
    public class BOrder:Common.CDBase
    {
        BOrderDetails _BOrderDetail = new BOrderDetails();
        public bool Add(OOrder _OOrder, List<OOrderDetails> lstOderDetail)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@IDCustomer",SqlDbType.Int)
                                      , new SqlParameter("@OrderStatus",SqlDbType.VarChar)
                                      , new SqlParameter("@TotalRevenueAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TotalProfitAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TotalPaidAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TotalDiscountAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TaxAmount",SqlDbType.Decimal)                                      
                                      , new SqlParameter("@CreateDate",SqlDbType.DateTime)
                                      , new SqlParameter("@PaidDate",SqlDbType.DateTime)};
            sqlParam[0].Value = _OOrder.IDCustomer;
            sqlParam[1].Value = _OOrder.OrderStatus;
            sqlParam[2].Value = _OOrder.TotalRevenueAmount;
            sqlParam[3].Value = _OOrder.TotalProfitAmount;
            sqlParam[4].Value = _OOrder.TotalPaidAmount;
            sqlParam[5].Value = _OOrder.TotalDiscountAmount;
            sqlParam[6].Value = _OOrder.TaxAmount;
            sqlParam[7].Value = _OOrder.CreateDate;
            sqlParam[8].Value = _OOrder.PaidDate;
            if (RunProcedure("sp_tblOrder_add", sqlParam))
            {
                int IDOrder = this.GetIDLastRecord();                
                if (IDOrder > 0)
                {
                    if (lstOderDetail != null)
                    {
                        foreach (OOrderDetails _OOrderDetail in lstOderDetail)
                        {
                            _BOrderDetail.Add(_OOrderDetail);    
                        }
                    }
                    return true;
                }
            }
            return false;
        }
        
        public IList<OOrder> Get(int OrderID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@OrderID", SqlDbType.Int) };
            sqlParam[0].Value = OrderID;
            return Common.Common.ConvertTo<OOrder>(RunProcedureGet("sp_tblOrder_get", sqlParam));
        }
        public IList<OOrder> Get(string Order, string OrderBy, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};            
            sqlParam[0].Value = Order;
            sqlParam[1].Value = OrderBy;
            sqlParam[2].Value = PageIndex;
            sqlParam[3].Value = PageSize;
            return Common.Common.ConvertTo<OOrder>(RunProcedureGet("sp_tblOrder_get", sqlParam));
        }
        public IList<OOrder> Get(int IDCustomer,string OrderStatus, DateTime FromCreateDate, DateTime ToCreateDate, string Order, string OrderBy, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@IDCustomer", SqlDbType.Int),
                                        new SqlParameter("@OrderStatus",SqlDbType.VarChar),
                                        new SqlParameter("@FromCreateDate",SqlDbType.DateTime),
                                        new SqlParameter("@ToCreateDate",SqlDbType.DateTime),                                        
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = IDCustomer;
            sqlParam[1].Value = OrderStatus;
            sqlParam[2].Value = FromCreateDate;
            sqlParam[3].Value = ToCreateDate;
            sqlParam[4].Value = Order;
            sqlParam[5].Value = OrderBy;
            sqlParam[6].Value = PageIndex;
            sqlParam[7].Value = PageSize;
            return Common.Common.ConvertTo<OOrder>(RunProcedureGet("sp_tblOrder_get", sqlParam));
        }
        /// <summary>
        /// Update Order Status
        /// </summary>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool Update(int OrderID,string Status)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@OrderID",SqlDbType.Int),
                                        new SqlParameter("@OrderStatus",SqlDbType.VarChar)};
            sqlParam[0].Value = OrderID;
            sqlParam[1].Value = Status;
            if(RunProcedure("sp_tblOrder_update_status", sqlParam)){return true;}
            return false;
        }
        public bool Update(OOrder _OOrder, List<OOrderDetails> lstOderDetail)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@OrderID",SqlDbType.Int)
                                      , new SqlParameter("@OrderStatus",SqlDbType.VarChar)
                                      , new SqlParameter("@TotalRevenueAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TotalProfitAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TotalPaidAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TotalDiscountAmount",SqlDbType.Decimal)
                                      , new SqlParameter("@TaxAmount",SqlDbType.Decimal)                                      
                                      , new SqlParameter("@CreateDate",SqlDbType.DateTime)
                                      , new SqlParameter("@PaidDate",SqlDbType.DateTime)};
            sqlParam[0].Value = _OOrder.OrderID;
            sqlParam[1].Value = _OOrder.OrderStatus;
            sqlParam[2].Value = _OOrder.TotalRevenueAmount;
            sqlParam[3].Value = _OOrder.TotalProfitAmount;
            sqlParam[4].Value = _OOrder.TotalPaidAmount;
            sqlParam[5].Value = _OOrder.TotalDiscountAmount;
            sqlParam[6].Value = _OOrder.TaxAmount;
            sqlParam[7].Value = _OOrder.CreateDate;
            sqlParam[8].Value = _OOrder.PaidDate;
            if (RunProcedure("sp_tblOrder_update", sqlParam))
            {
                _BOrderDetail.Delete(_OOrder.OrderID,0);
                if (lstOderDetail != null)
                {
                    foreach (OOrderDetails _OOrderDetail in lstOderDetail)
                    {
                        _BOrderDetail.Add(_OOrderDetail);
                    }
                }
                return true;
            }
            return false;
        }
        
        public int GetIDLastRecord()
        {
            DataTable dtb = RunProcedureGet("sp_tblOrder_getidlastrecord");
            if (dtb.Rows.Count > 0)
            {
                return int.Parse(dtb.Rows[0]["OrderID"].ToString());
            }
            return 0;
        }
        
        public bool Delete(int OrderID)
        {            
            SqlParameter[] sqlParam = { new SqlParameter("@OrderID", SqlDbType.Int) };
            sqlParam[0].Value = OrderID;
            return RunProcedure("sp_tblOrder_delete", sqlParam);
        }
    }
}
